package com.iwhalecloud.bss.kite.cucc.service.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.callclient.service.IOrderQueryService;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.entity.InfService;
import com.iwhalecloud.bss.kite.common.log.KiteLogEnum;
import com.iwhalecloud.bss.kite.common.log.KiteLogger;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IOrderService;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IRegistrationService;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.*;
import com.iwhalecloud.bss.kite.cucc.client.dto.webService.CustContractRsp;
import com.iwhalecloud.bss.kite.cucc.client.dto.webService.RegistrationRsp;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.callclient.constant.CallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalInfConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.CommonUtil;
import com.iwhalecloud.bss.kite.cucc.common.util.DecimalCalc;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceHelper;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.iwhalecloud.bss.kite.cucc.service.util.PayUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.ztesoft.bss.base.staticdata.CoreDataOffer;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.svcability.util.ListUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RegistrationService implements IRegistrationService {
    @Autowired
    IInfServiceBus infServiceBus;
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    @Override
    public PageInfo<RegistrationRsp> registrationInfo(Map map, @KiteNotBlank int pageNum,
                                                      @KiteNotBlank int pageSize) {
        Map<String, Object>   registrationMap = infServiceBus.registrationInfo(map, pageNum, pageSize);
        int TOTAL= (int) registrationMap.get("TOTAL");
        List  registrationList= (List) registrationMap.get("DATA");
        if(!ListUtil.isEmpty(registrationList)){

        }
        List<RegistrationRsp>  registrationLists= (List) registrationList.stream().map(s ->{
            JSONObject st=JSONObject.parseObject(JSON.toJSONString(s).toLowerCase());
            return  st.getJSONObject("cust_real_name_info");
        }).collect(Collectors.toList());
        PageInfo<RegistrationRsp> pageInfo = new PageInfo<RegistrationRsp>();
        pageInfo.setList(registrationLists);
        pageInfo.setTotal(TOTAL);
        pageInfo.setPageNum(Integer.parseInt(KiteMapUtils.getString(registrationMap,"PAGE_NUM")));
        pageInfo.setPageSize(Integer.parseInt(KiteMapUtils.getString(registrationMap,"PAGE_SIZE")));
        return pageInfo;
    }
    @Override
    public DataResult<Map> registrationDetail(String usercode, String iotTagCode,String sceneInstId,List<KiteAttr> commonAttrs) {
        List<KiteAttr> publicAttrs = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, LocalKeyConsts.MODULE_ID_REGISTRATION);
        Map<String, Object>   registrationMap = infServiceBus.registrationInfoDetail(usercode, iotTagCode);
        Map custMap= (Map) registrationMap.get("CUST_REAL_NAME");
        String IOT_TAG_CODE=KiteMapUtils.getString(custMap,"IOT_TAG_CODE");
        String IS_REAL=KiteMapUtils.getString(custMap,"IS_REAL");
        if("1".equals(IS_REAL)){
            IS_REAL="已生效";
        }else if("0".equals(IS_REAL)){
            IS_REAL="未生效";
        }
        Map IOT_RESPONSIBLE_INFO= (Map) registrationMap.get("IOT_RESPONSIBLE_INFO");
        String PHONE=KiteMapUtils.getString(IOT_RESPONSIBLE_INFO,"PHONE");
        String CERTI_ADDR=KiteMapUtils.getString(IOT_RESPONSIBLE_INFO,"CERTI_ADDR");
        String RESPONSIBLE_NAME=KiteMapUtils.getString(IOT_RESPONSIBLE_INFO,"RESPONSIBLE_NAME");
        String EMAIL=KiteMapUtils.getString(IOT_RESPONSIBLE_INFO,"EMAIL");
        String CERTI_TYPE_CODE=KiteMapUtils.getString(IOT_RESPONSIBLE_INFO,"CERTI_TYPE_CODE");
        String CERTI_CODE=KiteMapUtils.getString(IOT_RESPONSIBLE_INFO,"CERTI_CODE");
        if("1".equals(CERTI_TYPE_CODE)){
            CERTI_TYPE_CODE="身份证18位";
        }
        Map IOT_TRANSACTOR_INFO= (Map) registrationMap.get("IOT_TRANSACTOR_INFO");
        String iot_PHONE=KiteMapUtils.getString(IOT_TRANSACTOR_INFO,"PHONE");
        String iot_CERTI_ADDR=KiteMapUtils.getString(IOT_TRANSACTOR_INFO,"CERTI_ADDR");
        String TRANSACTOR_NAME=KiteMapUtils.getString(IOT_TRANSACTOR_INFO,"TRANSACTOR_NAME");
        String iot_EMAIL=KiteMapUtils.getString(IOT_TRANSACTOR_INFO,"EMAIL");
        String iot_CERTI_TYPE_CODE=KiteMapUtils.getString(IOT_TRANSACTOR_INFO,"CERTI_TYPE_CODE");
        if("1".equals(iot_CERTI_TYPE_CODE)){
            iot_CERTI_TYPE_CODE="身份证18位";
        }
        String iot_CERTI_CODE=KiteMapUtils.getString(IOT_TRANSACTOR_INFO,"CERTI_CODE");
       for(KiteAttr attr:publicAttrs){
           if("ORD10293".equals(attr.getFieldName())){
               attr.setValue(IOT_TAG_CODE);
               attr.setValueDesc(IOT_TAG_CODE);
           }else if("is_realis".equals(attr.getFieldName())){
               attr.setValue(IS_REAL);
               attr.setValueDesc(IS_REAL);
           }else if("duit_responsible_name".equals(attr.getFieldName())){
               attr.setValue(RESPONSIBLE_NAME);
               attr.setValueDesc(RESPONSIBLE_NAME);
           }else if("duit_certi_type_code".equals(attr.getFieldName())){
               attr.setValue(CERTI_TYPE_CODE);
               attr.setValueDesc(CERTI_TYPE_CODE);
           }else if("duit_certi_code".equals(attr.getFieldName())){
               attr.setValue(CERTI_CODE);
               attr.setValueDesc(CERTI_CODE);
           }else if("duit_certi_addr".equals(attr.getFieldName())){
               attr.setValue(CERTI_ADDR);
               attr.setValueDesc(CERTI_ADDR);
           }else if("duit_certi_phone".equals(attr.getFieldName())){
               attr.setValue(PHONE);
               attr.setValueDesc(PHONE);
           }else if("duit_certi_email".equals(attr.getFieldName())){
               attr.setValue(EMAIL);
               attr.setValueDesc(EMAIL);
           }else if("iot_transactor_name".equals(attr.getFieldName())){
               attr.setValue(TRANSACTOR_NAME);
               attr.setValueDesc(TRANSACTOR_NAME);
           }else if("iot_certi_type_code".equals(attr.getFieldName())){
               attr.setValue(iot_CERTI_TYPE_CODE);
               attr.setValueDesc(iot_CERTI_TYPE_CODE);
           }else if("iot_certi_code".equals(attr.getFieldName())){
               attr.setValue(iot_CERTI_CODE);
               attr.setValueDesc(iot_CERTI_CODE);
           }else if("iot_certi_addr".equals(attr.getFieldName())){
               attr.setValue(iot_CERTI_ADDR);
               attr.setValueDesc(iot_CERTI_ADDR);
           }else if("iot_certi_phone".equals(attr.getFieldName())){
               attr.setValue(iot_PHONE);
               attr.setValueDesc(iot_PHONE);
           }else if("iot_certi_email".equals(attr.getFieldName())){
               attr.setValue(iot_EMAIL);
               attr.setValueDesc(iot_EMAIL);
           }
           if(KiteListUtils.isNotEmpty(commonAttrs)){
               List<KiteAttr> targetAttrs = commonAttrs.stream().filter(attrs -> KiteStringUtils.equals(attr.getFieldName(), attrs.getFieldName())).collect(Collectors.toList());
               if (!KiteListUtils.isEmpty(targetAttrs)) {
                   KiteAttr targetAttr = targetAttrs.get(0);
                   targetAttr.setValue(attr.getValue());
                   targetAttr.setValueDesc(attr.getValueDesc());
                   targetAttr.setOriginValue(attr.getValue());
               }
           }else{
               operateSceneInstService.setCommonAttr(sceneInstId, attr);
           }
       }
        return null;
    }
}
